//
//  LGTRWZSubmitOrderModel.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/26.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import Foundation

//var submitDataInfo2:Dictionary<String, Any?> = ["type":nil, //选择类型
//    "os":nil,  //支持设备
//    "projectName":"", //项目名称
//    "taskRequest":"", //任务要求
//    "workTime":nil, //做单时间
//    "reviewTime":nil, //审核时间
//    "workTimes":nil, //做单次数
//    "workPrice":"", //任务单价
//    "taskCount":"", //任务数量
//    "toptime":"", //置顶天数
//    "offline":"", //下线时间
//    "step":[],//添加步骤
//    "payMoney":0.0, //总共花费点费用
//    "topPrice":0.0 //置顶钱
//]

struct LGTRWZSubmitOrderModel:Codable {
    var os:PostNameModel?
    var type:PostNameModel?
    var projectName:String?
    var taskRequest:String?
    var workTime:PostNameModel?
    var reviewTime:PostNameModel?
    var workTimes:PostNameModel?
    var workPrice:String?
    var taskCount:String?
    var toptime:String?
    var offline:String?
    var step:Array<LGTRWZStepOrderModel>? = []
    var payMoney:CGFloat?
    var topPrice:CGFloat?
}

struct LGTRWZStepOrderModel:Codable{
    var index:Int?  //0上传验证图  1上传说明图 2上传二维码
    var stepText:String?
    var collection:Bool? //手机信息
    var imageAndText:Bool?
    var selectInfo:LGTImageModel? //图片信息
    var subText:String?
    var isurl:Bool? //url
    var copy:Bool?  //复制数据
    var path:LGTPathModel?
    var type:Int? //步骤类型
}

struct LGTPathModel:Codable{
    var row:Int?
    var section:Int?
}

//图片数据信息
struct LGTImageModel:Codable{
    var height:CGFloat?
    var width:CGFloat?
    var size:CGFloat?
    var value:String?
    var id:Int?
    var type:String?
    var url:String?
}

func rwz_saveSubmitModel(info:Dictionary<String, Any?>){
    var submitModel:LGTRWZSubmitOrderModel = LGTRWZSubmitOrderModel()
    submitModel.type = info["type"] as? PostNameModel
    submitModel.os = info["os"] as? PostNameModel
    submitModel.projectName = info["projectName"] as? String
    submitModel.taskRequest = info["taskRequest"] as? String
    submitModel.workTime = info["workTime"] as? PostNameModel
    submitModel.reviewTime = info["reviewTime"] as? PostNameModel
    submitModel.workTimes = info["workTimes"] as? PostNameModel
    submitModel.workPrice = info["workPrice"] as? String
    submitModel.taskCount = info["taskCount"] as? String
    submitModel.toptime = info["toptime"] as? String
    submitModel.offline = info["offline"] as? String
    submitModel.payMoney = info["payMoney"] as? CGFloat
    submitModel.topPrice = info["topPrice"] as? CGFloat
    
    //步骤
    var stepModelArray:Array<LGTRWZStepOrderModel> = []
    var stepArray:Array<Dictionary<String, Any?>> = info["step"] as? Array<Dictionary<String, Any?>> ?? []
    for i in 0..<stepArray.count{
        var d:Dictionary<String, Any?> = stepArray[i]
        //path nsindexpath
        let path:NSIndexPath? = d["path"] as? NSIndexPath
        d["path"] = nil
        let selectInfo:Dictionary<String, Any>? = d["selectInfo"] as? Dictionary<String, Any>
        d["selectInfo"] = nil
        var stepModel:LGTRWZStepOrderModel =  try! lgtDecode(LGTRWZStepOrderModel.self, param: (d as [String : Any?]))
        if path != nil{
            var pathModel:LGTPathModel =  LGTPathModel()
            pathModel.row = path?.row
            pathModel.section = path?.section
            stepModel.path = pathModel
        }
        
        if selectInfo != nil{
            let imageModel:LGTImageModel =  try! lgtDecode(LGTImageModel.self, param: (selectInfo as! [String : Any?]))
            stepModel.selectInfo = imageModel
        }
        stepModelArray.append(stepModel)
    }
    submitModel.step = stepModelArray
    //var submitDataInfo2:Dictionary<String, Any?> = ["type":nil, //选择类型
    //    "os":nil,  //支持设备
    //    "projectName":"", //项目名称
    //    "taskRequest":"", //任务要求
    //    "workTime":nil, //做单时间
    //    "reviewTime":nil, //审核时间
    //    "workTimes":nil, //做单次数
    //    "workPrice":"", //任务单价
    //    "taskCount":"", //任务数量
    //    "toptime":"", //置顶天数
    //    "offline":"", //下线时间
    //    "step":[],//添加步骤
    //    "payMoney":0.0, //总共花费点费用
    //    "topPrice":0.0 //置顶钱
    //]
    
    //数据存储
    let encoder = JSONEncoder()
    let result = try! encoder.encode(submitModel)
    do{
        let json = try JSONSerialization.jsonObject(with: result, options: .mutableContainers) as! [String:Any]
        LGTUserUtil.share.saveSumitOrderModel(data: json)
    }
    catch{
        print(error)
    }
}

func rwz_getSubmitInfo() -> Dictionary<String, Any?>{
    let submitInfo:Dictionary<String, Any?> = LGTUserUtil.share.getSubmitOrderModel() ?? [:]
    var submitModel:LGTRWZSubmitOrderModel = try! lgtDecode(LGTRWZSubmitOrderModel.self, param: (submitInfo as [String : Any]))
    
    var submitDataInfo:Dictionary<String, Any?> = [:]
    submitDataInfo["os"] = submitModel.os
    submitDataInfo["type"] = submitModel.type
    submitDataInfo["projectName"] = submitModel.projectName
    submitDataInfo["taskRequest"] = submitModel.taskRequest
    submitDataInfo["workTime"] = submitModel.workTime
    submitDataInfo["reviewTime"] = submitModel.reviewTime
    submitDataInfo["workTimes"] = submitModel.workTimes
    submitDataInfo["workPrice"] = submitModel.workPrice
    submitDataInfo["taskCount"] = submitModel.taskCount
    submitDataInfo["toptime"] = submitModel.toptime
    submitDataInfo["offline"] = submitModel.offline
    submitDataInfo["payMoney"] = submitModel.payMoney
    submitDataInfo["topPrice"] = submitModel.topPrice
    
//    let encoder = JSONEncoder()
//    let result = try! encoder.encode(submitModel.step)
    var stepArray:Array<Dictionary<String, Any?>> = []
    for i in 0..<(submitModel.step?.count ?? 0){
        let model:LGTRWZStepOrderModel = submitModel.step![i]
        let encoder = JSONEncoder()
        let result = try! encoder.encode(model)
        do{
            var json:Dictionary<String, Any?> = try JSONSerialization.jsonObject(with: result, options: .mutableContainers) as! [String:Any?]
            if model.path != nil{
                var path:NSIndexPath = NSIndexPath.init(row: model.path?.row ?? 0, section: model.path?.section ?? 0)
                json["path"] = path
            }
            stepArray.append(json)
        }
        catch{
                continue
        }
    }
    submitDataInfo["step"] = stepArray
    return submitDataInfo
}
